#include<iostream>
#include<unistd.h>
#include<pthread.h>


int cnt = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;



void* Count(void* args)
{
    pthread_detach(pthread_self());
    uint64_t number = (uint64_t)args;
    while(true)
    {
        pthread_mutex_lock(&mutex);
        pthread_cond_wait(&cond,&mutex);                //pthread_cond_wait让线程等待时,会自动释放锁
        std::cout<<"number:  "<<number<<"  cnt:"<<cnt++<<std::endl;
        pthread_mutex_unlock(&mutex);
    }
}

int main()
{
    for(uint64_t i = 0;i<5;i++)
    {
        pthread_t tid;
        pthread_create(&tid,nullptr,Count,(void*)i);
    }

    while(true)
    {
        sleep(1);
        pthread_cond_signal(&cond);      //唤醒在cond队列中等待的第一个线程

    }

    return 0;
}
